package com.neulogistics.core.customercenter.domain.model.enumeration;

import com.neulogistics.core.customercenter.domain.exception.CustomerCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class OrderStatus extends Enumeration {

    public static final int Created_Id = 0;
    public static final int Out_Of_Stock_Id = 1;
    public static final int To_Be_Scheduled_Id = 2;
    public static final int Waiting_Out_Of_Warehouse_Id = 3;
    public static final int In_Transit_Id = 4;
    public static final int Arrive_At_Substation_Id = 5;
    public static final int Delivering_Id = 6;
    public static final int Completed_Id = 7;
    public static final int Returned_Id = 8;
    public static final int Exchanged_Id = 9;

    public static OrderStatus Created = new OrderStatus(Created_Id, "Created");
    public static OrderStatus Out_Of_Stock = new OrderStatus(Out_Of_Stock_Id, "Out_Of_Stock");
    public static OrderStatus To_Be_Scheduled = new OrderStatus(To_Be_Scheduled_Id, "To_Be_Scheduled");
    public static OrderStatus Waiting_Out_Of_Warehouse = new OrderStatus(Waiting_Out_Of_Warehouse_Id, "Waiting_Out_Of_Warehouse");
    public static OrderStatus In_Transit = new OrderStatus(In_Transit_Id, "In_Transit");
    public static OrderStatus Arrive_At_Substation = new OrderStatus(Arrive_At_Substation_Id, "Arrive_At_Substation");
    public static OrderStatus Delivering = new OrderStatus(Delivering_Id, "Delivering");
    public static OrderStatus Completed = new OrderStatus(Completed_Id, "Completed");
    public static OrderStatus Returned = new OrderStatus(Returned_Id, "Returned");
    public static OrderStatus Exchanged = new OrderStatus(Exchanged_Id, "Exchanged");


    protected OrderStatus(int id, String name) {
        super(id, name);
    }

    private static List<OrderStatus> list = Arrays.asList(
            Created,
            Out_Of_Stock,
            To_Be_Scheduled,
            Waiting_Out_Of_Warehouse,
            In_Transit,
            Arrive_At_Substation,
            Delivering,
            Completed,
            Returned,
            Exchanged);

    public static List<OrderStatus> getList(){return list;}

    public static OrderStatus fromName(String name) throws CustomerCenterDomainException {
        Optional<OrderStatus> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new CustomerCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            OrderStatus.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
